// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraHealExecution.h"
#include "AbilitySystem/Attributes/LyraHealthSet.h"
#include "AbilitySystem/Attributes/LyraCombatSet.h"

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraHealExecution)

// 治疗计算静态数据
struct FHealStatics
{
	FGameplayEffectAttributeCaptureDefinition BaseHealDef; // 基础治疗属性捕获定义

	FHealStatics()
	{
		BaseHealDef = FGameplayEffectAttributeCaptureDefinition(ULyraCombatSet::GetBaseHealAttribute(), EGameplayEffectAttributeCaptureSource::Source, true);
	}
};

// 获取治疗静态数据
static FHealStatics& HealStatics()
{
	static FHealStatics Statics;
	return Statics;
}

// 构造函数
ULyraHealExecution::ULyraHealExecution()
{
	RelevantAttributesToCapture.Add(HealStatics().BaseHealDef); // 添加基础治疗属性到相关捕获属性列表
}

/**
 * 执行治疗计算的具体实现
 */
void ULyraHealExecution::Execute_Implementation(const FGameplayEffectCustomExecutionParameters& ExecutionParams, FGameplayEffectCustomExecutionOutput& OutExecutionOutput) const
{
#if WITH_SERVER_CODE
	const FGameplayEffectSpec& Spec = ExecutionParams.GetOwningSpec(); // 获取拥有的游戏效果规格

	const FGameplayTagContainer* SourceTags = Spec.CapturedSourceTags.GetAggregatedTags(); // 获取源标签容器
	const FGameplayTagContainer* TargetTags = Spec.CapturedTargetTags.GetAggregatedTags(); // 获取目标标签容器

	FAggregatorEvaluateParameters EvaluateParameters; // 聚合器评估参数
	EvaluateParameters.SourceTags = SourceTags; // 设置源标签
	EvaluateParameters.TargetTags = TargetTags; // 设置目标标签

	float BaseHeal = 0.0f; // 基础治疗值
	ExecutionParams.AttemptCalculateCapturedAttributeMagnitude(HealStatics().BaseHealDef, EvaluateParameters, BaseHeal); // 尝试计算捕获属性的大小

	const float HealingDone = FMath::Max(0.0f, BaseHeal); // 计算最终造成的治疗量

	if (HealingDone > 0.0f) // 检查治疗量是否大于0
	{
		// 应用治疗修饰符，这将在目标上转换为正健康值
		OutExecutionOutput.AddOutputModifier(FGameplayModifierEvaluatedData(ULyraHealthSet::GetHealingAttribute(), EGameplayModOp::Additive, HealingDone)); // 添加输出修饰符
	}
#endif // #if WITH_SERVER_CODE
}